﻿import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQml 2.15
import Qt.labs.platform 1.1
import FluentUI 1.0

FluWindow {
    id:root
    minimumHeight: 400
    minimumWidth: 550
    launchMode: FluWindowType.SingleInstance
    appBar: FluAppBar {
        height: 30
        closeClickListener: (button)=>{
                                ControllerManager.towerDevice.disConnectDebuggerLink()
                                close()
                            }

        z:7
    }
    ColumnLayout {
        id :display
        RowLayout{
            FluMultilineTextBox{
                id:input_box
                Layout.margins: 10
                Layout.preferredHeight: root.height / 2.5
                Layout.preferredWidth: root.width / 1.5
            }
            Column{
                spacing: 10
                FluText{
                  text:qsTr("命令选择")
                }
                FluComboBox{
                    id:data_bits_combo
                    editable: false
                    model:SettingsHelper.getDebuggerList()
                    onCurrentTextChanged: {
                        input_box.text = SettingsHelper.getCmdByKey(currentText)
                    }
                }
                FluFilledButton{
                    text:qsTr("数据发送")
                    onClicked: {
                       ControllerManager.towerDevice.writeBytesToDebugger(input_box.text)
                    }
                }
            }
        }

        FluFrame {
            Layout.margins: 10
            Layout.preferredHeight:  root.height / 2.5
            Layout.preferredWidth: root.width/1.1


            ListView {
                id: logView
                anchors.fill: parent
                clip: true
                flickableDirection: Flickable.VerticalFlick  // 确保只能垂直滚动
                boundsBehavior: Flickable.StopAtBounds  // 在边界处停止滚动
                property bool autoScroll: true  // 自定义属性，用于控制是否自动滚动
                ScrollBar.vertical: ScrollBar {
                    // 竖向滚动条的配置
                    active: true  // 设置为激活状态，或根据需要进行显示或隐藏
                }
                model: ListModel {
                        id: display_model
                }
                delegate: FluText {
                    width: display_model.width
                    text: model.text
                    padding: 5
                    color: model.textColor
                }
                onContentYChanged: {
                    if (logView.flicking || logView.moving) {
                        logView.autoScroll = false;
                    }
                    // 检查是否滚动到了底部
                    if (logView.contentY >= logView.contentHeight - logView.height) {
                        logView.autoScroll = true;
                    }
                }
                function addNewDisPlayLog(color ,str) {
                    display_model.append({ text:str , textColor:color});
                    if (logView.autoScroll) {
                        // 确保滚动发生在项目被添加后
                        logView.positionViewAtIndex(display_model.count - 1, ListView.End)
                    }
                }
                Connections{
                    target:ControllerManager.towerDevice
                    function  onReadDebuggerMsg(str){
                        display_model.append({ text:str , textColor:"green"});
                        if (logView.autoScroll) {
                            // 确保滚动发生在项目被添加后
                            logView.positionViewAtIndex(display_model.count - 1, ListView.End)
                        }
                    }
                }
                Connections{
                    target:ControllerManager.towerDevice
                    function  onWriteDebggerMsg(str){
                        display_model.append({ text:str , textColor:"blue"});
                        if (logView.autoScroll) {
                            // 确保滚动发生在项目被添加后
                            logView.positionViewAtIndex(display_model.count - 1, ListView.End)
                        }
                    }

                }

                //                Timer {
//                    interval: 1000  // 设置定时器的间隔为1000毫秒，即1秒
//                    repeat: true  // 设置定时器重复运行
//                    running: true  // 定时器开始运行
//                    onTriggered: {
//                        // 定时器触发时执行的操作
//                        display_model.append({ text: "Automatic log entry at " + new Date().toLocaleTimeString() });
//                        if (logView.autoScroll) {
//                            // 确保滚动发生在项目被添加后
//                            logView.positionViewAtIndex(display_model.count - 1, ListView.End)
//                        }
//                    }
//                }
            }

//            FluText {
//                id: text
//                width: parent.width
//                wrapMode: Text.Wrap
//                Connections{
//                    target:ControllerManager.towerDevice
//                    function onReadDebuggerMsg(str) {
//                        text.text = str
//                    }
//                }

//            }
        }
    }
}
